package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.entity.FileManagement;
import com.wangsd.web.coupon.entity.ServiceBusiness;
import com.wangsd.web.coupon.mapper.CompanyMapper;
import com.wangsd.web.coupon.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.web.coupon.service.FileManagementService;
import com.wangsd.web.coupon.service.ServiceBusinessService;
import com.wangsd.web.sys.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-19
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    CompanyMapper companyMapper;

    @Autowired
    ServiceBusinessService serviceBusinessService;

    @Autowired
    FileManagementService fileManagementService;

    @Override
    @Transactional
    public Integer addOrUpdate(Company company) {
        int result = 0;
        if (company.getId() != null) {
            result = companyMapper.updateById(company);
        } else {
            result = companyMapper.insert(company);

            //保留检索CODE
            ServiceBusiness serviceBusiness = serviceBusinessService.getById(company.getBusinessServiceId());
            company.setCode(serviceBusiness.getCode() + "-" + company.getId());
            companyMapper.updateById(company);
        }
        return result;
    }

    @Override
    public IPage<Company> selectPage(String code, Integer pageNum, Integer pageSize, Company company) {
        Page<Company> page = new Page<>(pageNum, pageSize);
        return companyMapper.selectListXml(page, code, company);
    }

    @Override
    public List<Map> selectAllByUserCode(String code) {
        List<Map> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        List<Company> list = companyMapper.selectList(queryWrapper);
        for (Company sb : list) {
            Map map = new HashMap();
            map.put("id", sb.getId());
            map.put("name", sb.getName());
            map.put("code", sb.getCode());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public Company selectRepeat(String name, Long id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return companyMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public void addFileConfig(FileManagement fileManagement) {
        /*删除图片数据*/
        fileManagementService.deleteByMasterIdAndType(fileManagement);
        /*新增数据*/
        fileManagementService.saveBatch(fileManagement.getFileManagementList());
    }

    @Override
    public Company selectByMiniAppid(String miniAppid) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mini_appid",miniAppid);
        return companyMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Company> selectAllByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Company> list = companyMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Company> selectByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<Company> list = companyMapper.selectList(queryWrapper);
        return list;
    }
}
